﻿using Database;
using Qiniu.Http;
using Qiniu.Storage;
using Qiniu.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DSkinTheme.Helper
{
    public class QiNiuHelper
    {
        private ManualResetEvent doneEvent;
        /// <summary>
        /// 账号信息表
        /// </summary>
        private static string MTableName = "Users";

        /// <summary>
        /// 校验账号有效性
        /// </summary>
        /// <param name="accessKey"></param>
        /// <param name="secretKey"></param>
        /// <param name="domain"></param>
        /// <returns></returns>
        public static StatResult CheckAccount(string accessKey, string secretKey, string domain)
        {

            //check ak & sk validity
            Mac mac = new Mac(accessKey, secretKey);
            //use fixed zone to avoid the uc query
            Config config = new Config();

            Config.DefaultRsHost = domain;
            config.Zone = new Zone
            {
                RsHost = domain,
            };
            BucketManager bucketManager = new BucketManager(mac, config);
            StatResult statResult = bucketManager.Stat("NONE_EXIST_BUCKET", "NONE_EXIST_KEY");
            return statResult;
        }

        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="bucketName"></param>
        /// <param name="accessKey"></param>
        /// <param name="secretKey"></param>
        /// <param name="domain"></param>
        /// <param name="fileName"></param>
        public static string UploadFiles(DbAccess db, string fileName)
        {

            var data = db.SelectWhere(MTableName, new string[] { "*" }, new DbCompareEqual("Type", "1"));
            string bucketName = string.Empty; string accessKey = string.Empty;
            string secretKey = string.Empty; string domain = string.Empty;
            while (data.Read())
            {
                bucketName = data.GetString(0);
                accessKey = data.GetString(1);
                secretKey = data.GetString(2);
                domain = data.GetString(3);

                Mac mac = new Mac(accessKey, secretKey);
                //set bucket manager and upload manager config
                Config config = new Config();
                config.UseCdnDomains = true;
                config.Zone = ZoneHelper.QueryZone(accessKey, bucketName);


                UploadManager uploadManager = new UploadManager(config);

                PutExtra putExtra = new PutExtra();

                UploadProgressHandler progressHandler = new UploadProgressHandler(delegate (long uploadBytes, long totalBytes)
                {
                    Console.WriteLine("progress: " + uploadBytes + ", " + totalBytes);
                    double percent = uploadBytes * 1.0 / totalBytes;
                    //this.syncProgressPage.updateSingleFileProgress(taskId, fileFullPath, fileKey, fileLength, percent);
                });
                putExtra.ProgressHandler = progressHandler;

                //putExtra.UploadController = new UploadController(delegate
                //{
                //    //if (this.syncProgressPage.FinishSignal)
                //    //{
                //        return UploadControllerAction.Aborted;
                //    //}
                //    //else
                //    //{
                //    //    if (this.syncProgressPage.CancelSignal)
                //    //    {
                //    //        return UploadControllerAction.Suspended;
                //    //    }
                //    //    else
                //    //    {
                //    //        return UploadControllerAction.Activated;
                //    //    }
                //    //}
                //});
                PutPolicy putPolicy = new PutPolicy();
                putPolicy.Scope = bucketName;
                putPolicy.SetExpires(7200);

                string uptoken = Auth.CreateUploadToken(mac, putPolicy.ToJsonString());

                HttpResult uploadResult = uploadManager.UploadFile(fileName, "1218/" + DateTime.Now.ToString("yyyyMMddHHmmssfffffff"), uptoken, putExtra);
                if (uploadResult.Code != 200)
                {

                }
                return "";
            }
            return "";
        }
    }
}
